package com.topInterview;

/**
 * @Author: huangzhigao
 * @Date: 2022/2/13 16:09
 */
public class Leecode14_LongestCommonPrefix {

    //查询每个字符串的公共部分，然后取最小
    public String longestCommonPrefix(String[] strs) {
        if (strs == null || strs.length < 1) {
            return "";
        }

        char[] chars1 = strs[0].toCharArray();
        int min = Integer.MAX_VALUE;
        for (int i = 0; i < strs.length; i++) {
            char[] chars = strs[i].toCharArray();
            int index = 0;
            while (index < chars.length && index < chars1.length) {
                if (chars[index] != chars1[index]) {
                    break;
                }
                if (min == 0) {
                    return "";
                }
                index++;
            }
            min = Math.min(index, min);
        }

        return strs[0].substring(0, min);
    }
}
